import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemOperateLogApi } from '#/api/system/operate-log';
import type { DescriptionItemSchema } from '#/components/description';

import { $t } from '#/locales';
import { formatDateTime } from '@vben/utils';

import { getSimpleUserList } from '#/api/system/user';
import { getRangePickerDefaultProps } from '#/utils';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'userId',
      label: $t('views.system.operateLog.username'),
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleUserList,
        fieldNames: {
          label: 'nickname',
          value: 'id',
        },
        allowClear: true,
        placeholder: $t('views.system.operateLog.userIdPlaceholder'),
      },
    },
    {
      fieldName: 'type',
      label: $t('views.system.operateLog.type'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.system.operateLog.typePlaceholder'),
      },
    },
    {
      fieldName: 'subType',
      label: $t('views.system.operateLog.subType'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.system.operateLog.subTypePlaceholder'),
      },
    },
    {
      fieldName: 'action',
      label: $t('views.system.operateLog.action'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.system.operateLog.actionPlaceholder'),
      },
    },
    {
      fieldName: 'createTime',
      label: $t('views.system.operateLog.createTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'bizId',
      label: $t('views.system.operateLog.bizId'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.system.operateLog.bizIdPlaceholder'),
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: $t('views.system.operateLog.id'),
      minWidth: 100,
    },
    {
      field: 'userName',
      title: $t('views.system.operateLog.username'),
      minWidth: 120,
    },
    {
      field: 'type',
      title: $t('views.system.operateLog.type'),
      minWidth: 120,
    },
    {
      field: 'subType',
      title: $t('views.system.operateLog.subType'),
      minWidth: 160,
    },
    {
      field: 'action',
      title: $t('views.system.operateLog.action'),
      minWidth: 200,
    },
    {
      field: 'createTime',
      title: $t('views.system.operateLog.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'bizId',
      title: $t('views.system.operateLog.bizId'),
      minWidth: 120,
    },
    {
      field: 'userIp',
      title: $t('views.system.operateLog.userIp'),
      minWidth: 120,
    },
    {
      title: $t('common.action'),
      width: 100,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 详情页的字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      field: 'id',
      label: $t('views.system.operateLog.id'),
    },
    {
      field: 'traceId',
      label: $t('views.system.operateLog.traceId'),
      hidden: (data: SystemOperateLogApi.OperateLog) => !data?.traceId,
    },
    {
      field: 'userId',
      label: $t('views.system.operateLog.userId'),
    },
    {
      field: 'userName',
      label: $t('views.system.operateLog.username'),
    },
    {
      field: 'userIp',
      label: $t('views.system.operateLog.userIp'),
    },
    {
      field: 'userAgent',
      label: $t('views.system.operateLog.userAgent'),
    },
    {
      field: 'type',
      label: $t('views.system.operateLog.type'),
    },
    {
      field: 'subType',
      label: $t('views.system.operateLog.subType'),
    },
    {
      field: 'action',
      label: $t('views.system.operateLog.action'),
    },
    {
      field: 'extra',
      label: $t('views.system.operateLog.extra'),
      hidden: (data: SystemOperateLogApi.OperateLog) => !data?.extra,
    },
    {
      label: $t('views.system.operateLog.requestUrl'),
      content: (data: SystemOperateLogApi.OperateLog) => {
        if (data?.requestMethod && data?.requestUrl) {
          return `${data.requestMethod} ${data.requestUrl}`;
        }
        return '';
      },
    },
    {
      field: 'createTime',
      label: $t('views.system.operateLog.createTime'),
      content: (data: SystemOperateLogApi.OperateLog) => {
        return formatDateTime(data?.createTime || '') as string;
      },
    },
    {
      field: 'bizId',
      label: $t('views.system.operateLog.bizId'),
    },
  ];
}
